package com.quan.web.controller.weixin;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.quan.common.annotation.RequiresPermissions;
import com.quan.common.base.pojo.FileBean;
import com.quan.common.base.pojo.JSONResult;
import com.quan.common.base.pojo.PageBean;
import com.quan.common.core.support.BaseController;
import com.quan.common.utils.FileTools;
import com.quan.common.utils.FileUpload;
import com.quan.sdk.weixin.tools.MediaTools;
import com.quan.weixin.rpc.api.WeixinNewsImgService;
import com.quan.weixin.rpc.model.WeixinNewsImg;


@RestController
@RequestMapping(value = "/api/weixin/news_img")
public class WeixinNewsImgController extends BaseController {

	@Autowired
	private WeixinNewsImgService weixinNewsImgService;

	@RequiresPermissions(value="weixin:news_img:add", description="添加微信图文素材图片")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody WeixinNewsImg entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinNewsImgService.insertSelective(entity);

		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:news_img:delete", description="删除微信图文素材图片")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.weixinNewsImgService.deleteById(Integer.valueOf(id.toString()));
		return JSONResult.ok();
	}
	
	@RequiresPermissions(value="weixin:news_img:delete", description="删除微信图文素材图片")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.weixinNewsImgService.deleteByIds(ids) ;
		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:news_img:update", description="修改微信图文素材图片")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody WeixinNewsImg entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinNewsImgService.update(entity);
		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:news_img:detail", description="查询微信图文素材图片详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.weixinNewsImgService.selectOneById(id);
	}
	
	@RequiresPermissions(value="weixin:news_img:list", description="查询微信图文素材图片")
	@GetMapping(value = "/list")
	public Object list(WeixinNewsImg entity) {
		return this.weixinNewsImgService.selectPage(entity, new PageBean(request)) ;
	}
	
	/**
	 * 上传图文消息内的图片
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/uploadNewsImg")
	public Object uploadNewsImg(HttpServletRequest request, String remarks) {
		
		FileBean fileBean = FileUpload.upload(request, "/weixin/news/img") ;
		
		// TODO 图片压缩
		
		JSONObject jsonObject = MediaTools.uploadNewsImg(new File(fileBean.getFullPath()));
		String imgUrl = jsonObject.get("url").toString() ;
		
		WeixinNewsImg newsImg = new WeixinNewsImg() ;
		newsImg.setRemarks(remarks);
		newsImg.setUrl(imgUrl);
		this.weixinNewsImgService.insertSelective(newsImg) ;
		
		FileTools.deleteFile(fileBean.getFullPath());
		
		return JSONResult.ok() ;
	}
	
}
